﻿var CurrentStep = "Login";
$(document).ready(function ()
{
    //alert(CurrentStep);
    $('#idLoginWaiting').hide();
    loginFunBtnClick();
    switch (CurrentStep)
    {
        case "Login":
            $('#idLogin2System').click(function ()
            {
                VerifyLogin();
            });
            FetchStoredData();
            break;
        case "ForgotPassword":
            break;
        case "Signup":
            break;
    }

    $('#idForgotPassword').click(function ()
    {
        CurrentStep = "ForgotPassword";
        $('#idLoginContent').hide();
        $('#idFunc').load('forgotPassword.htm', function ()
        {
            $('#idLoginWaiting').hide();
        });
        $('#idFunc').show();
        $('#idBtnFunc').html('Sign In');
    });
});

function FetchStoredData()
{
    $.ajax({
        type: "POST",
        url: "Login.ashx",
        data: "mode=fetchData",
        async: true,
        success: function (data)
        {
            var r = data.split('|');
            $('#idUserId').attr('value', r[0]);
            $('#idPassword').attr('value', r[1]);
        }
    });
}
function isValidMail(sText)
{
    var reMail = /@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$/;
    return reMail.test(sText);
}
function VerifyLogin()
{
    var r = $('#idUserId').val();
    var p = $('#idPassword').val();
    var isRememberPassword = $('#idRememberPassword').attr("checked");
    
    if (isRememberPassword == "checked")
    {
        isRememberPassword = true;
    }
    else
    {
        isRememberPassword = false;
    }
    $.ajax({
        type: "POST",
        url: "Login.ashx",
        data: "mode=VerifyLogin&email=" + r + "&p=" + hex_md5(p) + "&checked=" + isRememberPassword + "&o=" + p,
        async: true,
        success: function (data)
        {
            if (data == 'True')
            {
                $('#idError').hide();
                relocate(r);
            }
            else
            {
                $('#idError').html('Wrong Email and password combination.');
                $('#idError').show();
            }
        }
    });
}

//get the id according to the email address
function relocate(email)
{
    $.ajax({
        type: "POST",
        url: "Login.ashx",
        data: "mode=fetchId&email=" + email,
        async: false,
        success: function (data)
        {
            if (data != '0')
            {
                self.location = "../index.htm"
            }
            else
            {
                alert('Server error, please contact administrator!');
            }
        }
    });
}

function loginFunBtnClick()
{
    $('#idBtnFunc').click(function ()
    {
        //alert(CurrentStep);
        $('#idLoginWaiting').show();
        switch (CurrentStep)
        {
            case "Login":
                CurrentStep = "Signup";
                $('#idLoginContent').hide();
                $('#idFunc').load('signup.htm', function ()
                {
                    $('#idLoginWaiting').hide();
                });
                $('#idFunc').show();
                $('#idBtnFunc').html('Sign In');
                break;
            case "Signup":
            case "ForgotPassword":
                CurrentStep = "Login";
                $('#idFunc').hide();
                $('#idLoginContent').show();
                $('#idBtnFunc').html('Sign Up');
                $('#idLoginWaiting').hide();
                break;
        }
    });
}